package cn.itcast.foodapp2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.gson.JsonIOException;

import org.json.JSONException;
import org.json.JSONObject;

import cn.itcast.foodapp2.net.NetUtil;

public class RegisterActivity extends AppCompatActivity {
    private static final String TAG = "RegisterActivity";
    TextView mTvUserName, mTvPassword, mTvTelephone;
    Button btnSubmit, btnCancel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
        initEvent();
    }

    private void initView() {
        mTvUserName = findViewById(R.id.ed_food_name_register);
        mTvPassword = findViewById(R.id.ed_food_password_register);
        mTvTelephone = findViewById(R.id.ed_food_telephone_register);
        btnSubmit = findViewById(R.id.btn_submit_user_register);
        btnCancel = findViewById(R.id.btn_cancel_user_register);
    }

    private void initEvent() {
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String name = mTvUserName.getText().toString() + "";
                        String password = mTvPassword.getText().toString() + "";
                        String telephone = mTvTelephone.getText().toString() + "";
                        JSONObject jsonObject = new JSONObject();
                        try {
                            jsonObject.put("name", name);
                            jsonObject.put("password", password);
                            jsonObject.put("telephone", telephone);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
//                        final String data = NetUtil.getData(jsonObject, "/user/register");
                        String url = "http://42.48.39.55:8081/foodserver/user/register";
                        final String data = NetUtil.getData(jsonObject, url);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Log.e(TAG, "runOnUiThread: " + data);
                            }
                        });
                    }
                }).start();
            }
        });
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                startActivity(intent);
            }
        });
    }
}